package com.itheima.advantagesup.controller;

import com.itheima.advantagesup.common.R;
import com.itheima.advantagesup.dto.MenuDTO;
import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.service.SysMenuService;
import com.itheima.advantagesup.vo.MenuVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单控制器
 *
 * @author liudo
 * @version 1.0
 * @project advantages-up
 * @date 2023/9/9 19:55:59
 */
@RestController
@RequestMapping("/sys_menu")
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService sysMenuService;

    /**
     * 所有数据
     *
     * @return {@link R}<{@link List}<{@link MenuVO}>>
     */
    @GetMapping("/allData")
    public R<List<MenuVO>> allData() {
        List<MenuVO> menuVOList = sysMenuService.allData();
        return R.success(menuVOList);
    }

    /**
     * 行信息
     *
     * @param id id
     * @return {@link R}<{@link MenuVO}>
     */
    @GetMapping("/rowInfo")
    public R<MenuVO> rowInfo(Integer id) {
        MenuVO menuVO = sysMenuService.rowInfo(id);
        return R.success(menuVO);
    }

    /**
     * 使现代化
     *
     * @param sysMenuDTO 系统菜单dto
     * @return {@link R}<{@link ?}>
     */
    @PostMapping("/update")
    public R<?> update(@RequestBody MenuDTO sysMenuDTO) {
        SysMenu sysMenu = sysMenuDTO.convertToEntity();
        sysMenuService.update(sysMenu);
        return R.success();
    }

    /**
     * 添加
     *
     * @param menuDTO 菜单dto
     * @return {@link R}<{@link ?}>
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody MenuDTO menuDTO) {
        SysMenu sysMenu = menuDTO.convertToEntity();
        sysMenuService.save(sysMenu);
        return R.success();
    }
}
